package com.yanqu.road.server.cmd.activity.newyear2023.redpacket;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.FireWorkProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.newyear2023.FireWorkActivity;
import com.yanqu.road.server.manager.activity.newyear2023.FireWorkMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_FIREWORK_ACTIVITY_ENTER, desc = "年夜饭进入红包现场")
public class CrossFireWorkEnterCmd extends CrossCommand {

    private int activityId;
    private int no;
    private long userId;
    private UserBaseInfo userBaseInfo;
    private boolean isChangeUserBaseInfo = false;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        FireWorkProto.CrossFireWorkEnterRedPacketReqMsg reqMsg = FireWorkProto.CrossFireWorkEnterRedPacketReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        no = reqMsg.getNo();
        userId = packet.getPlayerId();
        if(reqMsg.hasPlayerBaseData()){
            userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
            isChangeUserBaseInfo = CrossUserMgr.updateUserBaseInfoChange(userId,userBaseInfo);
        }
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        FireWorkActivity activity = FireWorkMgr.getFireWorkActivity(activityId);
        if (activity == null) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }

        activity.enterUser(serverId, userId,no);
        if(isChangeUserBaseInfo){
            activity.changeUserBaseUpdate(serverId, userId);
        }
    }

    @Override
    public void sendData() {
        if(ret != 0){
            FireWorkProto.FireWorkEnterRedPacketHomeRespMsg.Builder respMsg = FireWorkProto.FireWorkEnterRedPacketHomeRespMsg.newBuilder();
            respMsg.setRet(ret);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.U_FIREWORK_RP_ENTER, respMsg);
            MessageHelper.sendPacket(serverId, userId, pbMsg);
        }
    }

}
